package com.itheima.demo3;

import com.itheima.demo2.Student;

import java.lang.reflect.Field;

public class Demo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Student student = new Student("张三",25);
        Object name = Demo.getProperty(student, "name");
        System.out.println(name);
    }

    public static Object getProperty(Object obj,String propertyName) throws NoSuchFieldException, IllegalAccessException {
        Class aClass = obj.getClass();
        Field declaredField = aClass.getDeclaredField(propertyName);
        declaredField.setAccessible(true);
        return declaredField.get(obj);
    }
}
